package com.jl.card.parse.part;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.jl.card.parse.IAPDUComponent;

public abstract class APDUPart implements IAPDUComponent {

	public final static int TYPE_P1P2 = 0x01;
	public final static int TYPE_CDATA = 0x02;
	public final static int TYPE_RDATA = 0x03;
	public final static int TYPE_SW = 0x04;
	
	public final static int TYPE_TLV = 0x06;

	byte[] part;
	IAPDUComponent parent;
	List<IAPDUComponent> children = new ArrayList<IAPDUComponent>();

	public APDUPart(IAPDUComponent parent, byte[] part) {
		this.parent = parent;
		this.part = part == null ? new byte[] {} : part.clone();
		if(parent != null){
			parent.add(this);
		}
	}
	
	@Override
	public abstract void analyse();
	
	@Override
	public abstract String getSummary();

	@Override
	public Iterator<IAPDUComponent> getIterator() {
		return new Iterator<IAPDUComponent>() {
			int index = 0;
			@Override
			public boolean hasNext() {
				return index < children.size();
			}

			@Override
			public IAPDUComponent next() {
				return children.get(index++);
			}

			@Override
			public void remove() {
			}
		};
	}

	@Override
	public IAPDUComponent getParent() {
		return parent;
	}

	@Override
	public Object[] childrenToArray() {
		return children.toArray(new IAPDUComponent[]{});
	}

	@Override
	public boolean hasChildren() {
		return children.size() != 0;
	}

	@Override
	public void add(IAPDUComponent apduPart) {
		children.add(apduPart);
	}

}
